<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
	User me = userByUid(token.getUid());
	if (me == null || !Tools.getDesc(me.getJson("role"), "1").equals("3")) {
		Tools.encryptPrint(out, new Message(30031, "您无权限进行该操作"));
		return;
	}
	Integer deptId = me.getDeptId();
	if (deptId == null) {
		Tools.encryptPrint(out, new Message(30041, "不存在该科室"));
		return;
	}
	Integer memberUid = input.getInt("member_uid");
	if (memberUid.equals(token.getUid())) {
		Tools.encryptPrint(out, new Message(30031, "您不能修改自己的信息"));
		return;
	}
	List<User> list = QBuilder.db("basedb").select().from("User").where(new Where("hospital=", hospital).and("deptId=", deptId).and("status=", STATUS_ENABLE).and("type=", USER_EMPLOYEE).and("id=", memberUid)).query().getResultObj();
	if (isEmpty(list)) {
		Tools.encryptPrint(out, new Message(30031, "本科室不存在该用户"));
		return;
	}
	User member = list.get(0);
	String name = input.getDesc("name", "");

	String regexStr = "^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$";
	if (!name.matches(regexStr)) {
		msg = new Message(30041, "姓名只能为中文汉字");
		return;
	}
	if (name.length() > 10 || name.length() < 2 ) {
		msg = new Message(30041, "姓名为2-10位汉字");
		Tools.encryptPrint(out, msg);
		return;
	}
	member.setName(name);
	String phone = input.getDesc("phone", "");
	if (!isChinaPhoneLegal(phone)) {
		Tools.encryptPrint(out, new Message(30041, "手机号不合法"));
		return ;
	}
	List<User> phoneList = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("u.phone=", phone).and("u.type=", USER_EMPLOYEE).and("u.id!=", memberUid)).page(1, 1).query().getResultObj();
	if (!isEmpty(phoneList)) {
		Tools.encryptPrint(out, new Message(30031, "该手机号已被绑定"));
		return ;
	}
	member.setPhone(phone);

	Integer role = input.getInt("role", -1);
	if (-1 != role) {
		member.setJson("role", role + "");
	}
	Integer gender = input.getInt("gender", -1);
	if (-1 != gender) {
		member.setGender(gender);
	}
	String employeeId = input.getDesc("employee_id", "");
	if (!"".equals(employeeId)) {
		List<User> employeelist = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("u.employeeId=", employeeId).and("u.type=", USER_EMPLOYEE).and("u.id!=", memberUid)).page(1, 1).query().getResultObj();
		if (!isEmpty(employeelist)) {
			Tools.encryptPrint(out, new Message(30031, "该工号已被绑定"));
			return ;
		}
		member.setEmployeeId(employeeId);
	}
	if ("".equals(employeeId)) {
		member.setEmployeeId(employeeId);
	}
	String title  = input.getDesc("title", "");
	member.setTitle(filterAll(title));
	String password = input.getDesc("password", "");
	if (!"".equals(password)) {
		member.setPassword(password);
	}
	String position = input.getDesc("position", "");
	member.setJson("position", filterAll(position));
	String teachTitle = input.getDesc("teach_title", "");
	member.setJson("teach_title", filterAll(teachTitle));

	DBTools.db("basedb").modItem(member, member.getId());
	Tools.encryptPrint(out, msg);
%>
